package protocol

import (
	"xr-game-server/core/constants/common"
	"xr-game-server/gamecfg"
)

type ItemModel struct {
	Id uint64 `json:"id" dc:"物品id"`
	//添加数量
	Val uint64 `json:"val" dc:"值"`
	//旧值
	Old uint64 `json:"-"`
	//当前数量
	Current uint64 `json:"-"`
	//失败数量
	Fail   uint64                 `json:"-"`
	Source *gamecfg.ItemSourceCfg `json:"-"`
	TypeId uint32                 `json:"-"`
}

func NewItemModels(items [][]uint64) []*ItemModel {
	itemModels := make([]*ItemModel, common.Zero)
	for _, val := range items {
		sourceCfg := gamecfg.GetItemSourceById(val[2])
		itemModels = append(itemModels, &ItemModel{
			Id:     val[0],
			Val:    val[1],
			Source: sourceCfg,
		})

	}
	return itemModels
}

func NewMailItem(id uint64, val uint64) *ItemModel {
	return &ItemModel{
		Id:  id,
		Val: val,
	}
}

// ItemHandler 物品处理器
type ItemHandler interface {
	Consume(roleId uint64, items []*ItemModel, canPush bool) bool
	AddItem(roleId uint64, item []*ItemModel, canPush bool)
	CanConsume(roleId uint64, item *ItemModel) bool
}

const (
	// Res 资源类型处理器
	Res = 1
	// Bag 背包
	Bag = 2
	// Exp 经验
	Exp = 3
)

var handlerMap = make(map[uint32]ItemHandler)

func GetItemHandlerBy(typeId uint32) ItemHandler {
	handler, ok := handlerMap[uint32(typeId)]
	if !ok {
		return nil
	}
	return handler
}

func RegItemHandler(typeId uint32, handler ItemHandler) {
	handlerMap[typeId] = handler
}
